package com.gym.web.controller.appointment.controller;

import java.util.Date;
import java.util.List;

import com.gym.common.core.domain.entity.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gym.common.annotation.Log;
import com.gym.common.enums.BusinessType;
import com.gym.system.appointment.domain.GymAppointment;
import com.gym.system.appointment.service.IGymAppointmentService;
import com.gym.common.core.controller.BaseController;
import com.gym.common.core.domain.AjaxResult;
import com.gym.common.utils.poi.ExcelUtil;
import com.gym.common.core.page.TableDataInfo;

/**
 * 预约Controller
 *
 * @author administrator
 * @date 2021-04-11
 */
@Controller
@RequestMapping("/appointment/appointment")
public class GymAppointmentController extends BaseController {
    private String prefix = "appointment/appointment";

    @Autowired
    private IGymAppointmentService gymAppointmentService;

    @RequiresPermissions("appointment:appointment:view")
    @GetMapping()
    public String appointment() {
        return prefix + "/appointment";
    }

    /**
     * 查询预约列表
     */
    @RequiresPermissions("appointment:appointment:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GymAppointment gymAppointment) {
        startPage();
        List<GymAppointment> list = gymAppointmentService.selectGymAppointmentList(gymAppointment);
        return getDataTable(list);
    }

    /**
     * 导出预约列表
     */
    @RequiresPermissions("appointment:appointment:export")
    @Log(title = "预约", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GymAppointment gymAppointment) {
        List<GymAppointment> list = gymAppointmentService.selectGymAppointmentList(gymAppointment);
        ExcelUtil<GymAppointment> util = new ExcelUtil<GymAppointment>(GymAppointment.class);
        return util.exportExcel(list, "appointment");
    }

    /**
     * 新增预约
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存预约
     */
    @RequiresPermissions("appointment:appointment:add")
    @Log(title = "预约", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GymAppointment gymAppointment) {

        if (StringUtils.isBlank(gymAppointment.getAppointUser())) {
            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
            gymAppointment.setAppointUser(sysUser.getUserName());
        }
        if (gymAppointment.getStartTime() == null) {
            gymAppointment.setStartTime(new Date());
        }
        return toAjax(gymAppointmentService.insertGymAppointment(gymAppointment));
    }

    /**
     * 修改预约
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        GymAppointment gymAppointment = gymAppointmentService.selectGymAppointmentById(id);
        mmap.put("gymAppointment", gymAppointment);
        return prefix + "/edit";
    }

    /**
     * 修改保存预约
     */
    @RequiresPermissions("appointment:appointment:edit")
    @Log(title = "预约", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GymAppointment gymAppointment) {
        return toAjax(gymAppointmentService.updateGymAppointment(gymAppointment));
    }

    /**
     * 删除预约
     */
    @RequiresPermissions("appointment:appointment:remove")
    @Log(title = "预约", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(gymAppointmentService.deleteGymAppointmentByIds(ids));
    }
}
